package com.study.talk.ui.app.moment

import android.os.Bundle
import android.view.Menu
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.study.talk.R
import com.study.talk.data.repository.MomentRepository
import com.study.talk.ui.app.base.BaseActivity
import com.study.talk.databinding.ActivityMomentsBinding
import com.study.talk.ui.adapter.moment.MomentsAdapter
import com.study.talk.utils.extras.getIntParam
import com.study.talk.utils.extras.start
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject


@AndroidEntryPoint
class MomentsActivity : BaseActivity() {

    companion object {
        const val MOMENT_ALL: Int = 0
        const val MOMENT_FRIEND: Int = 1
        const val MOMENT_SELF: Int = 2
        const val TYPE_NAME: String = "MOMENT_LIST_TYPE"
        const val DATE_NAME: String = "USER_ID"
    }

    @Inject
    lateinit var momentRepository: MomentRepository

    private val binding: ActivityMomentsBinding by lazy {
        ActivityMomentsBinding.inflate(
            layoutInflater
        )
    }

    private val vm: MomentsViewModel by viewModels()

    private val adapter: MomentsAdapter by lazy { MomentsAdapter(app, momentRepository) }

    private val momentsType: Int by lazy { getIntParam(TYPE_NAME) }

    private val userId: Int by lazy { getIntParam(DATE_NAME) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        setSupportActionBar(binding.momentListToolbar)
        initView()
    }


    //如果是在只看朋友的朋友圈，就不显示添加朋友圈的menu
    override fun onPrepareOptionsMenu(menu: Menu): Boolean {
        when (momentsType) {
            MOMENT_FRIEND -> menu.findItem(R.id.moment_menu_item).setVisible(false)
            MOMENT_ALL, MOMENT_SELF -> menu.findItem(R.id.moment_menu_item).setVisible(true)
        }
        return super.onPrepareOptionsMenu(menu)
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.moment_menu, menu)
        return super.onCreateOptionsMenu(menu)
    }


    private fun initView() {

        binding.momentRecycleView.layoutManager = LinearLayoutManager(this)

        binding.momentRecycleView.adapter = adapter

        //加载头像
        loadUserAvatar(binding.momentListUserAvatar)

        //点击返回按钮
        binding.momentListToolbar.setNavigationOnClickListener { finish() }

        //选择图片新增朋友圈
        binding.momentListToolbar.setOnMenuItemClickListener {
            if (it.itemId == R.id.moment_menu_item) {
                start(AddMomentActivity::class.java)
            }
            true
        }

        vm.moments.observe(this) {
            adapter.submitList(it)
        }
    }


    private fun getData() {
        if (momentsType == MOMENT_ALL) {
            vm.getMoments(app.localUserId)
        } else {
            vm.getUserMoments(userId)
        }
    }

    override fun onStart() {
        super.onStart()
        getData()
    }


}